//Code for testing gencode()
//Tested By: Sukhbir on 2/14/11
//The test produces right results, finally!!

#include <iostream>	
#include <iomanip>

using namespace std; 

int len;
char rpn[]={34,35,3};//{34,35,36,7,37,38,7,5,39,40,7,41,42,7,5,7,3}; //a=b

void emit(char a, char n)
{
	char ext []={63, 63, 63, 61, 43, 45, 42, 47, 65, 66,
				  67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
				  77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
				  87, 88, 89, 90, 97, 98, 99, 100,101,102,
				  103,104,105,106,107,108,109,110,111,112,
				  113,114,115,116,117,118,119,120,121,122};
				  
	static char *op[]= { "***", "lod", "sto", "***", "add", "sub", "mul", "div" };
	
	cout<<setw(5)<<++len<<" "<<op[(int)a];
	
	if(n < 60){
		cout<<" "<<ext[(int)n];
	}
	else{ 
		cout<<" t.";
		if(n > 60)
			cout<<'+'<<n-60;
	}

	cout<<'\n';

}
int main(){
    
	char j, lop, *t, *p,  n1, n2, a;
	
	cout<<"Generated symbolic code:\n\n";
	j=len=lop=0;
	t=p=rpn;

while(3<(a=*++p))
{
	if(7<a){
	*++t=a;

		if((++lop==2) && (0<len)){
			n2=*t--;
			n1=*t--;
			*++t=j+60;
			*++t=n1;
			*++t=n2;
			emit(2,(j++)+60);
		}	
	}
	else{
		if(lop==1){
			emit(a,*t--);
		}
		else{
			if(1<lop){
				n2=*t--;
				n1=*t--;
				emit(1,n1);
				emit(a,n2);
			}
			else{
				if((a==4)||(a==6)){
					n1=*t--;
					emit(a,n1);
				}
				else{
					n1=*t--;
					emit(2,j+60);
					emit(1,n1);
					emit(a,j+60);
				}
			}
			if(59<n1){
				--j;
			}
		}
	lop=0;	
	}
		
}

	if(len==0){
		emit(1,rpn[1]);
	}
	emit(2,*rpn);
		
		return 0;
}



